package com.csii.branch.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.csii.core.util.ConvertUtils;
import com.csii.core.api.Result;
import com.csii.core.system.query.QueryGenerator;
import com.csii.core.util.oConvertUtils;
import com.csii.branch.mapper.entity.CdBranchStatDaily;
import com.csii.branch.service.ICdBranchStatDailyService;
import com.csii.branch.controller.vo.CdBranchStatDailyVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import com.csii.core.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.csii.core.annotation.AutoLog;

 /**
 * @Description: 网点每日统计
 * @Author: power-stdio
 * @Date:   2020-12-21
 * @Version: V1.0
 */
@Api(tags="网点每日统计")
@RestController
@RequestMapping("/cdBranchStatDaily")
@Slf4j
public class CdBranchStatDailyController extends BaseController<CdBranchStatDaily, ICdBranchStatDailyService> {
	@Autowired
	private ICdBranchStatDailyService cdBranchStatDailyService;

	 @ApiOperation(value="根据网点ID和日期获取每日统计", notes="网点每日统计-分页列表查询")
	 @GetMapping(value = "/find")
	 public Result<CdBranchStatDailyVO> find(String netId,String statDate){
		 CdBranchStatDaily branchStatDaily=cdBranchStatDailyService.getOne(new LambdaQueryWrapper<CdBranchStatDaily>().eq(CdBranchStatDaily::getNetId,netId).eq(CdBranchStatDaily::getStatDate, DateUtil.parse(statDate)));
		 if(branchStatDaily==null)
		 {
		 	return Result.error("未找到相关数据");
		 }
		 CdBranchStatDailyVO vo= ConvertUtils.convert(CdBranchStatDailyVO.class,branchStatDaily);
		 return Result.ok(vo );
	 }

	/**
	 * 分页列表查询
	 *
	 * @param cdBranchStatDailyVO
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "网点每日统计-分页列表查询")
	@ApiOperation(value="网点每日统计-分页列表查询", notes="网点每日统计-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CdBranchStatDailyVO cdBranchStatDailyVO,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		CdBranchStatDaily cdBranchStatDaily  = ConvertUtils.convert(CdBranchStatDaily.class,cdBranchStatDailyVO);
		QueryWrapper<CdBranchStatDaily> queryWrapper = QueryGenerator.initQueryWrapper(cdBranchStatDaily, req.getParameterMap());
		Page<CdBranchStatDaily> page = new Page<CdBranchStatDaily>(pageNo, pageSize);
		IPage<CdBranchStatDaily> pageList = cdBranchStatDailyService.page(page, queryWrapper);
		IPage<CdBranchStatDailyVO> pageListVo = ConvertUtils.convertIPage(CdBranchStatDailyVO.class, pageList);
		return Result.ok(pageListVo);
	}
	
	/**
	 *   添加
	 *
	 * @param cdBranchStatDailyVO
	 * @return
	 */
	@AutoLog(value = "网点每日统计-添加")
	@ApiOperation(value="网点每日统计-添加", notes="网点每日统计-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CdBranchStatDailyVO cdBranchStatDailyVO) {
		CdBranchStatDaily cdBranchStatDaily  = ConvertUtils.convert(CdBranchStatDaily.class,cdBranchStatDailyVO);
		cdBranchStatDailyService.save(cdBranchStatDaily);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param cdBranchStatDailyVO
	 * @return
	 */
	@AutoLog(value = "网点每日统计-编辑")
	@ApiOperation(value="网点每日统计-编辑", notes="网点每日统计-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CdBranchStatDailyVO cdBranchStatDailyVO) {
		CdBranchStatDaily cdBranchStatDaily  = ConvertUtils.convert(CdBranchStatDaily.class,cdBranchStatDailyVO);
		cdBranchStatDailyService.updateById(cdBranchStatDaily);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "网点每日统计-通过id删除")
	@ApiOperation(value="网点每日统计-通过id删除", notes="网点每日统计-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		cdBranchStatDailyService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "网点每日统计-批量删除")
	@ApiOperation(value="网点每日统计-批量删除", notes="网点每日统计-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.cdBranchStatDailyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "网点每日统计-通过id查询")
	@ApiOperation(value="网点每日统计-通过id查询", notes="网点每日统计-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CdBranchStatDaily cdBranchStatDaily = cdBranchStatDailyService.getById(id);
		if(cdBranchStatDaily==null) {
			return Result.error("未找到对应数据");
		}
		CdBranchStatDailyVO cdBranchStatDailyVO  = ConvertUtils.convert(CdBranchStatDailyVO.class,cdBranchStatDaily);
		return Result.ok(cdBranchStatDailyVO);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param cdBranchStatDailyVO
    */
    @PostMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CdBranchStatDailyVO cdBranchStatDailyVO) {
        CdBranchStatDaily cdBranchStatDaily  = ConvertUtils.convert(CdBranchStatDaily.class,cdBranchStatDailyVO);
        return super.exportXls(request, cdBranchStatDaily, CdBranchStatDaily.class, "网点每日统计");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CdBranchStatDaily.class);
    }
}
